"""
    Controller for editing new agreement, based on `student_migration_for_english_admin` view.
    Contains another mass actions than `StudentMigrationForAdmin` controller.
"""

from mnemoply import model
from mnemoply.mass_actions import MassAction, MassActionForm
from mnemoply.controllers.lookup import LookupController, TableCol, TableLookupDataSource
from mnemoply.controllers.StudentAgreement import AgreementController, EducationInfoTab
from mnemoply.controllers.crud import CRUDController
from mnemoply.widgets import DictionaryField
from mnemoply.utils import planSortFunction

class MAFillEnglishPlan(MassAction):
    target_class = model.StudentAgreement
    name = "fill_english_plan"
    title = _("Fill plans")

    form = MassActionForm(name=name,
        fields=[
            DictionaryField(
                name="plan_id",
                model_object_name='Plan',
                not_empty=True,
                sort_func=[planSortFunction],
                label=_("Plan"),
            ),
        ]
    )

    def call(self, objs, plan_id):
        plan = model.Plan.get(int(plan_id))

        for agr in objs:
            agr.initialPlan = plan

class MigrationForEnglishAdminController(LookupController, CRUDController):
    model_object = model.StudentAgreement

    lookup_columns = [
        TableCol(name='student', caption=_('Student')),
        TableCol(name='old_groups', caption=_('Old groups')),
        TableCol(name='school_form', caption=_('School form')),
        TableCol(name='group_title', caption=_('Group NEXT')),
        TableCol(name='student_wish_plan', caption=_('Initial plan student wish')),
        TableCol(name='initial_plan', caption=_('Result plan')),
        TableCol(name='possible_day', caption=_('Possible day'), hidden=True),
        TableCol(name='unpossible_day', caption=_('Unpossible day'), hidden=True),
        TableCol(name='office', caption=_('Office')),
        TableCol(name='test_result_basic', caption=_('Basic test result'), hidden=True),
        TableCol(name='test_result_prof', caption=_('Prof. test result'), hidden=True),
        TableCol(name='friends', caption=_('Friends'), hidden=True),
        TableCol(name='ei_comment', caption=_('Admin comment'), hidden=True),
        TableCol(name='payment', caption=_('Amount'), hidden=True),
        TableCol(name='payment_date', caption=_('Payment date'), hidden=True),
        TableCol(name='trend', caption=_('Trend'), hidden=True),
        ]

    edit_fields = AgreementController.edit_fields
    view_fields = AgreementController.view_fields
    item_tabs = [EducationInfoTab()]
    mass_actions_class_names = [
        'MAMigrationGroupInclusion',
        'MAGroupTransferWithoutOrder',
        'MAFillEnglishPlan',
        ]

    def __init__(self):
        LookupController.__init__(self,
            data_source=TableLookupDataSource('student_migration_for_english_admin'),
            columns=self.lookup_columns,
            title=self.model_object.model_title,
            mass_actions_class = model.StudentAgreement,
            mass_actions_class_names=self.mass_actions_class_names,
        )
        CRUDController.__init__(self, model_object=self.model_object)
        self.title = _("Migration for english admin")
        self.local_menu = []

exposed = MigrationForEnglishAdminController()
